package com.example.benefitcommunity.controller;

import com.example.benefitcommunity.entity.Result;
import com.example.benefitcommunity.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Tag(name = "全局搜索")
@CrossOrigin
@RestController
@RequestMapping("bcSearch")
public class BcSearchController {
    @Resource
    private BcAddressListService bcAddressListService;

    @Resource
    private BcCommunityService bcCommunityService;

    @Resource
    private BcHousingRentalService bcHousingRentalService;

    @Resource
    private BcIdleTradingService bcIdleTradingService;

    @Resource
    private BcLostThingService bcLostThingService;

    @Resource
    private BcMerchantService bcMerchantService;

    @Resource
    private BcNotificationAnnouncementService bcNotificationAnnouncementService;

    @Resource
    private BcParkingspaceRentService bcParkingspaceRentService;

    @Resource
    private BcServiceDemandsService bcServiceDemandsService;

    /**
     * 通过参数数据
     *
     * @param parameter 参数
     * @return 数据
     */
    @Operation(description = "查询")
    @GetMapping("search")
    public Result<Object> search(String parameter) {
        Result<Object> result;

        //通讯录
        result = bcAddressListService.queryByName(parameter);
        if (result.getData() instanceof Map) {
            Map<String, Object> dataMap = (Map<String, Object>) result.getData();
            Object addressListObj = dataMap.get("AddressList");
            if (addressListObj instanceof List && !((List<?>) addressListObj).isEmpty()) {
                return result;
            }
        }
        //社区管理
        result = bcCommunityService.queryByCommunityName(parameter);
        if (result.getData() instanceof Map) {
            Map<String, Object> dataMap = (Map<String, Object>) result.getData();
            Object addressListObj = dataMap.get("Community");
            if (addressListObj instanceof List && !((List<?>) addressListObj).isEmpty()) {
                return result;
            }
        }
        //房屋租赁
        result = bcHousingRentalService.queryByHouse(parameter);
        if (result.getData() instanceof Map) {
            Map<String, Object> dataMap = (Map<String, Object>) result.getData();
            Object addressListObj = dataMap.get("HousingRental");
            if (addressListObj instanceof List && !((List<?>) addressListObj).isEmpty()) {
                return result;
            }
        }
        //闲置交易
        result = bcIdleTradingService.queryByIdlename(parameter);
        if (result.getData() instanceof Map) {
            Map<String, Object> dataMap = (Map<String, Object>) result.getData();
            Object addressListObj = dataMap.get("IdleTrading");
            if (addressListObj instanceof List && !((List<?>) addressListObj).isEmpty()) {
                return result;
            }
        }
        //失物招领
        result = bcLostThingService.queryByTitle(parameter);
        if (result.getData() instanceof Map) {
            Map<String, Object> dataMap = (Map<String, Object>) result.getData();
            Object addressListObj = dataMap.get("LostThing");
            if (addressListObj instanceof List && !((List<?>) addressListObj).isEmpty()) {
                return result;
            }
        }
        //商户管理
        result = bcMerchantService.queryByName(parameter);
        if (result.getData() instanceof Map) {
            Map<String, Object> dataMap = (Map<String, Object>) result.getData();
            Object merchantObj = dataMap.get("Merchant");
            if (merchantObj instanceof List && !((List<?>) merchantObj).isEmpty()) {
                return result;
            }
        }
        //通知公告
        result = bcNotificationAnnouncementService.queryByTitle(parameter);
        if (result.getData() instanceof Map) {
            Map<String, Object> dataMap = (Map<String, Object>) result.getData();
            Object addressListObj = dataMap.get("NotificationAnnouncement");
            if (addressListObj instanceof List && !((List<?>) addressListObj).isEmpty()) {
                return result;
            }
        }
        //车位出租
        result = bcParkingspaceRentService.queryByNum(parameter);
        if (result.getData() instanceof Map) {
            Map<String, Object> dataMap = (Map<String, Object>) result.getData();
            Object addressListObj = dataMap.get("ParkingspaceRent");
            if (addressListObj instanceof List && !((List<?>) addressListObj).isEmpty()) {
                return result;
            }
        }
        //服务诉求
        result = bcServiceDemandsService.queryByType(parameter);
        if (result.getData() instanceof Map) {
            Map<String, Object> dataMap = (Map<String, Object>) result.getData();
            Object addressListObj = dataMap.get("ServiceDemands");
            if (addressListObj instanceof List && !((List<?>) addressListObj).isEmpty()) {
                return result;
            }
        }
        return result;
    }
}